#pragma once

#include <vector>

template <typename T>
class TRefCount {
public:
	TRefCount() { 
		addRef((T *)this); 
	}
	TRefCount(const TRefCount&) { 
		addRef((T *)this); 
	}
	~TRefCount() { 
		removeRef((T *)this); 
	}
	static size_t getInstanceCount() { 
		return mObjRefList.size(); 
	}
	void grab(void) {
		mRefCount++;
	}
	void release(void) {
		mRefCount--;
	}
private:
	static std::vector<T*> mObjRefList;
	static unsigned int mRefCount;

	static void addRef(T *r) {
		mObjRefList.push_back(r);
	}
	static void removeRef(T *r) {
		std::vector<T*>::iterator iter;
		
		for(iter=mObjRefList.begin();iter != mObjRefList.end(); iter++) {
			if(*iter==r) {
				mObjRefList.erase(iter);
				break;
			}
		}
	}
};
template<typename T> unsigned int TRefCount<T>::mRefCount = 0;
template<typename T> std::vector<T*> TRefCount<T>::mObjRefList;